/**
 * @author Samuel Gonzalez
 * @version 1.0.6 Thu May 10, 2012 10:04PM GMT-400
 */

/*
 * Copyright (C) 2011  Samuel Gonzalez, PR
 * BenchGraph. Generates graphics and HTML tables based on HTTPERF output.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * In addition, as a special exception, the copyright holders give permission
 * to link the code of this work with the OpenSSL project's "OpenSSL" library
 * (or with modified versions of it that use the same license as the "OpenSSL" 
 * library), and distribute linked combinations including the two.  You must
 * obey the GNU General Public License in all respects for all of the code
 * used other than "OpenSSL".  If you modify this file, you may extend this
 * exception to your version of the file, but you are not obligated to do so.
 * If you do not wish to do so, delete this exception statement from your
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
 */

import java.io.*;
import java.util.ArrayList;

public class Html {
	
	/**
	 * Call method in charge of html file creation
	 * @param filename
	 * @param tables
	 * @param directory
	 * @param imgNames
	 */
	public Html(String filename, ArrayList<String> tables, String directory, ArrayList<String> imgNames){
		generateHTML(filename, tables, directory, imgNames);
	}
	
	/**
	 * Generate html file with data provided.
	 * @param filename
	 * @param tables
	 * @param directory
	 * @param imgNames
	 */
	private void generateHTML(String filename, ArrayList<String> tables, String directory, ArrayList<String> imgNames){
		
		try{
			
			PrintWriter outFile = new PrintWriter(new File(filename));
			
			outFile.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
			outFile.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
			outFile.println("\t<head>");
			outFile.println("\t\t<title>BenchGraph: An HTML Graphical Tool for Performance Analysis of Web Server Benchmarks</title>");
			outFile.println("\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
			outFile.println("\t\t<!--[if IE]><style type=\"text/css\">body { text-align: center; } #wrapper { float: left; }</style><![endif]-->");
			outFile.println("\t</head>");
			outFile.println("\t<body>");
			outFile.println("\t\t<div id=\"wrapper\" style=\"width: 800px; margin: 0px auto 0px auto;\">");
			outFile.println("\t\t\t<div id=\"header\">");
			outFile.println("\t\t\t\t<div style=\"text-decoration: underline;\">BenchGraph: An HTML Graphical Tool for Performance Analysis of Web Server Benchmarks</div>");
			outFile.println("\t\t\t</div>\n\t\t\t<div style=\"clear:both;\">&nbsp;</div>");
			outFile.println("\t\t\t<div style=\"text-align:center;\">");
			
			for(int i = 0; i < tables.size(); i++){
				outFile.print(tables.get(i));
				outFile.println("<div style=\"clear:both;\">&nbsp;</div>");
			}
			
			// Display images
			if(imgNames != null){
				for(int i = 0; i < imgNames.size(); i++)
					outFile.println("\t\t\t\t<img style=\"clear: left;\" src=\"" + directory + imgNames.get(i) + "\" alt=\"\" />");
			}					
			outFile.println("\t\t\t</div>\n\t\t</div>\n\t</body>\n</html>");			
			outFile.close();
			
		}catch(SecurityException se){
			Console.wln("BenchGraph is not authorize to create directories here.");
			System.exit(1);
		}catch(NullPointerException npe){
			Console.wln("Path name is incorrect.");
			System.exit(1);
		}catch(FileNotFoundException fnfe){
			Console.wln("File not found.");
			System.exit(1);
		}
	}
}

